<!DOCTYPE html>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <title>绑定</title>
    <meta name="description" content="后台管理">
    <link href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
    <link href="../static/css/font-awesome.min.css" th:href="@{/css/font-awesome.min.css}" rel="stylesheet"/>
    <link href="../static/css/style.min.css" th:href="@{/css/style.min.css}" rel="stylesheet"/>
    <link href="../static/css/login.min.css" th:href="@{/css/login.min.css}" rel="stylesheet"/>
    <link href="../static/ruoyi/css/ry-ui.css" th:href="@{/ruoyi/css/ry-ui.css?v=4.7.0}" rel="stylesheet"/>
    <!-- 360浏览器急速模式 -->
    <meta name="renderer" content="webkit">
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="shortcut icon" href="../static/favicon.ico" th:href="@{favicon.ico}"/>
    <style type="text/css">label.error { position:inherit;  }</style>
</head>
<body class="signin">
<div class="signinpanel">
    <div class="row">
        <div class="col-sm-5">
            <form id="registerForm" autocomplete="off">
                <input type="text"  id="phone"   name="phone" class="form-control uname"         placeholder="手机号"   maxlength="20" />
                <button type="button" id="sendMsg"  class="btn btn-mini btn-warning" onclick="sendSms()">获取手机验证码</button>
                <input type="text" name="vCode" class="form-control code"         placeholder="验证码"     maxlength="20" />
                <button type="button" class="btn btn-success btn-block" id="btnSubmit" onclick="binding()" >绑定</button>
            </form>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">

    var openId;

    $(function() {
        let appid="wx5857869a96260c90";
        let code = getUrlParam('code');
        let local= window.location.href;
        $.modal.msgError(code);
        console.log(code);
        if(code===null||code===''){
            window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid
                +'&redirect_uri='+encodeURIComponent(local)+'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
        }else{
            $.modal.closeLoading();
            getOpenId(code);
        }
        // getOpenId(code);
    });

    function getUrlParam(name) {
        $.modal.alertError("getUrlParam"+name);
        var reg =new RegExp('(^|&)' +name + '=([^&]*)(&|$)');
        var r=window.location.search.substr(1).match(reg);
        if(r!=null){
            return unescape(r[2]);
        }else{
            return null;
        }
    }

    function getOpenId(code) {
        $.ajax({
            type:'get',
            url:"http://dxbtest.dianxiaobao.top:8086/dxb2/login/getOpenId?code="+code,
            data:null,
            contentType: "application/json",
            datatype:"json",
            success:function(result){
                if (result.code === web_status.SUCCESS) {
                    openId=result.data.msg;
                    $('#id').val(openId);
                } else {
                    alert(result.msg);
                }
            },
            error:function(status){
                console.log(status)
            }
        });
    }

    function sendSms() {
        $.modal.loading("正在发送");
        let phone = $('#phone').val().trim()
        $.ajax({
            type: "get",
            url: "http://dxbtest.dianxiaobao.top:8086/dxb2/login/sendMsg?phone="+phone+"&checkType=1",
            data: null,
            contentType: "application/json",
            dataType: 'JSON',
            success: function(result) {
                $.modal.closeLoading();
                $.modal.alertSuccess(result.msg);
            },
            error: function(error) {
                $.modal.closeLoading();
                $.modal.alertWarning("服务器出错，请联系客服");
            }
        });
    }
    function binding() {
        $.modal.loading("正在绑定");
        var phone = $.common.trim($("input[name='phone']").val());
        var vCode = $.common.trim($("input[name='vCode']").val());
        $.ajax({
            type: "get",
            url: "http://dxbtest.dianxiaobao.top:8086/dxb2/login/BindUser?phone="+phone+"&vCode="+vCode+"&openId="+openId,
            data: null,
            contentType: "application/json",
            dataType: 'JSON',
            success: function(result) {
                if (result.code === web_status.SUCCESS) {
                    $.modal.closeLoading();
                    $.modal.alertSuccess(result.msg);
                } else {
                    $.modal.closeLoading();
                    $.modal.alertError(result.msg);
                }
            },
            error: function(error) {
                $.modal.closeLoading();
                $.modal.alertWarning("服务器出错，请联系客服");
            }
        });
    }
</script>
<!-- 全局js -->
</body>
</html>
